<template>
  <div>
    <!-- 搜索 -->
    <!-- 紧急返件回运 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleEdit" :title="'紧急返件回运编辑'" destroyOnClose width="70%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="closeEdit" @save="saveEdit" :rowData="rowData"></EditForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ModelVehicle @close="close" @save="save"></ModelVehicle>
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import EditForm from './EditForm.vue';
import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { exportExcelFile } from '@/utils/index';
import { getListByPage, getPartType, getListForExport,exporturl } from '@/api/claim/dealer/QualityInformationManagement_DLR/EmergencyReturn/index';
import { dictionary } from '@/utils/dictMixin';
import { getAppId } from '@/assets/js/auth';

export default {
  mixins: [dictionary],
  components: {
    EditForm,
    ModelVehicle
  },
  data() {
    return {
      dataList: [],
      rowData: {},
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {
        techStatusStrs: '55651003,55651005, 55651007,55651008',
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      visibleEdit: false
    };
  },
  created() {
    this.chuqi();
    // this.fetchParams.beginExpressDate = moment()
    //   .startOf('week')
    //   .format('YYYY-MM-DD HH:mm:ss');
    // this.fetchParams.endExpressDate = moment()
    //   .endOf('week')
    //   .format('YYYY-MM-DD HH:mm:ss');
  },
  methods: {
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000213' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginExpressDate|endExpressDate').initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginExpressDate|endExpressDate').initialValue[1];
      // // if(!beginSubmitDate) {
      // //   this.$notify.warning({ title: '提示信息', message: '回运开始日期不可为空！' });
      // //   return
      // // }
      // // if(!endSubmitDate) {
      // //   this.$notify.warning({ title: '提示信息', message: '回运结束日期不可为空！' });
      // //   return
      // // }
      // if (getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '回运日期跨度不可超过十二个月！' });
      //     return;
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '回运日期跨度不可超过十二个月！' });
      //     return;
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { techStatusStrs: '55651003,55651005, 55651007,55651008', xhrAbort: false });
      // obj.beginExpressDate = this.topFilterInit.find(x => x.fieldName == 'beginExpressDate|endExpressDate').initialValue[0];
      // obj.endExpressDate = this.topFilterInit.find(x => x.fieldName == 'beginExpressDate|endExpressDate').initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '紧急返件回运导出');
      // });
    },
    async chuqi() {
      // const res = await getPartType({ type: 5565 });
      // if (res.resultCode == 200) {
      //   delete res.data[0];
      //   delete res.data[1];
      //   delete res.data[3];
      //   delete res.data[5];
      //   delete res.data[8];
      //   this.topFilterInit.find(x => x.fieldName == 'urgentStatus').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'urgentStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '紧急返件单号', fieldName: 'urgentNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '索赔单号', fieldName: 'cmsClaimNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '质量报告单号', fieldName: 'reportNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          readonly: true, // 只读
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()} />
              </div>
            );
          }
        },
        {
          type: 'SELECT',
          label: '紧急返件单状态',
          fieldName: 'urgentStatus',
          itemList: this.createDictList('5565', [55651001, 55651002, 55651004, 55651006, 55651009]),
          readonly: true // 只读
        },
        {
          type: 'RANGE_DATE',
          label: '回运时间',
          fieldName: 'beginExpressDate|endExpressDate',
          placeholder: '请输入',
          // initialValue: [
          //   moment()
          //     .startOf('week')
          //     .format('YYYY-MM-DD HH:mm:ss'),
          //   moment()
          //     .endOf('week')
          //     .format('YYYY-MM-DD HH:mm:ss')
          // ]
          // rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      // if (!val.beginExpressDate) {
      //   this.$notify.warning({ title: '提示信息', message: '回运开始日期不可为空！' });
      //   return;
      // }
      // if (!val.endExpressDate) {
      //   this.$notify.warning({ title: '提示信息', message: '回运结束日期不可为空！' });
      //   return;
      // }
      if (getAppId() == 'arcfox') {
        if (moment(moment(val.endExpressDate).format('YYYY-MM-DD')).diff(moment(val.beginExpressDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '回运日期跨度不可超过十二个月！' });
          return;
        }
      } else {
        if (moment(moment(val.endExpressDate).format('YYYY-MM-DD')).diff(moment(val.beginExpressDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '回运日期跨度不可超过十二个月！' });
          return;
        }
      }
      this.fetchParams = Object.assign({}, val, { techStatusStrs: '55651003,55651005, 55651007,55651008', xhrAbort: false });
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button
                  size="mini"
                  type="text"
                  disabled={props.row.assessStatus == '55451003' || props.row.assessStatus == '55451004' || props.row.assessStatus == '55451005' || props.row.assessStatus == '55451001' ? true : false}
                  onClick={() => this.edit(props.row)}
                >
                  回运
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '紧急返件单状态', dataIndex: 'statusStr', sorter: true },
        { title: '质量报告单号', dataIndex: 'reportNo', sorter: true },
        { title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车型名称', dataIndex: 'modelCode', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '维修时间', dataIndex: 'serviceBeginDate', sorter: true },
        { title: '邮寄人', dataIndex: 'expressUser', sorter: true },
        { title: '邮寄人电话', dataIndex: 'expressTel', sorter: true },
        { title: '收件地址', dataIndex: 'receiverAddress', sorter: true },
        { title: '回运时间', dataIndex: 'expressDate', sorter: true }
        // { title: '主键', dataIndex: 'urgentId', hidden: true }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    modelTurnOn() {
      this.visible = true;
    },
    edit(val) {
      this.visibleEdit = true;
      this.rowData = val;
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.visible = false;
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      //this.fetchParams = { ...this.fetchParams };
    }
  }
};
</script>

<style lang="less" scoped></style>
